
package com.webbei.Backstage.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.commons.collections.ArrayStack;
import com.webbei.Backstage.service.BackstateSer;
import com.webbei.pojo.Managers;
import com.webbei.pojo.Orders;
import com.webbei.pojo.Trade;
import com.webbei.util.Arithmetic;

public class SelectIndusByMany extends ActionSupport{
       //private int indusId;
	   private String alias;
	   private String orderId;
       private String uname;
       private String orderstate;
       private BackstateSer bao;
       private List<Orders> orderlist;
       private String startTime;
       private String endTime;
       private int page;
       private static final int MAX = 50;
       private int pagecount;
       private int total;
       private List<Trade> trades=null;
       
	   public String execute(){
		   System.out.println("selectIndusByMany....");
		   trades = new ArrayList<Trade>();
		   Managers mana = (Managers) ActionContext.getContext().getSession().get("mana");
		   int indusId = mana.getIndustryId();
		Map<String,Object>m = new HashMap<String, Object>();
		if(orderId!=null){
		orderId = orderId.trim();
		}
		if(uname!=null){
		uname = uname.trim();
		}
		if(alias!=null){
		alias = alias.trim();
		}
		if("".equals(orderstate)){
			orderstate=1+"";
		}
		if("-1".equals(orderstate)){
			orderstate=null;
		}
		if("".equals(startTime)){
			startTime=null;
		}
		if("".equals(endTime)){
		    endTime=null;	
		}
		if("".equals(uname)){
			uname=null;
		}
		if("".equals(alias)){
			alias=null;
		}
		
		if(alias!=null){
			String[]marks = alias.split("，|,|[ \t]+");
			alias = "";
			for(int i=0;i<marks.length;i++){
				String s = marks[i];
				if(i==0){
					alias += s;
				}else{
					alias+="|"+s;
				}
				
			}
		}
		System.out.println(orderId);
		try{
			Long id = Long.parseLong(orderId);
		}catch(NumberFormatException e){
			orderId = null;
		}
		m.put("tradeId", orderId);
		 m.put("startTime", startTime);
	      m.put("endTime", endTime);
	      m.put("indusid", indusId);
	      m.put("username", uname);
	      m.put("orstate", orderstate);
	      m.put("mark", alias);
		   total=bao.getOrdercount(m);
			   pagecount = (total% MAX) == 0 ? (total/MAX): (total/MAX+ 1);
				if (page> pagecount) {
					page=pagecount;
				}
				if (page< 1) {
					page = 1;
				}
				int start = (page - 1) * MAX;
				m.put("start", start);
				m.put("maxpage", MAX);
				 orderlist = bao.getorderByday(m);
				 System.out.println(orderlist.size()+"*************************");
				 int len=orderlist.size();
				 Arithmetic.getKey("webbei"); 
				 for(int i=0;i<len;i++){
					 Orders o = orderlist.get(i);
						List<Orders> orders = bao.getOrdersByTradeId(o.getTradeId());
						int size = orders.size();
						for(int j=0;j<size;j++){
							Orders or = orders.get(j);
							String cdrUrl = or.getCDRurl();
							String mark = or.getMark();
							String sku = or.getSku();
							Integer picId = or.getBackPicId();
							 if(cdrUrl!=null){
								 or.setCDRurl(Arithmetic.getEncString(cdrUrl));
							 }
							 int labelId=-1;
								if(picId!=null){
									labelId=bao.getLabelIdByPicId(picId);
								}
								if(mark!=null&&!("".equals(mark))){
									mark = "%"+mark+"%";
									Map<String,Object>map = new HashMap<String, Object>();
									map.put("mark", mark);
									if(sku!=null&&"".equals(sku)){
										sku="%"+sku+"%";
									}
									map.put("sku", sku);
									labelId = bao.getLableIdByMap(map);
								}
								if(labelId!=0){
									or.setLabelCosts(bao.getLabelCostByLabelId(labelId));
								}
						}
						Trade t = new Trade();
						t.setTradeId(o.getOrderId());
						t.setOrders(orders);
						t.setCount(orders.size());
						trades.add(t);
				 }
    	   return SUCCESS;
         }
	
	public int getTotal() {
		return total;
	}


	public void setTotal(int total) {
		this.total = total;
	}


	public BackstateSer getBao() {
		return bao;
	}
	public void setBao(BackstateSer bao) {
		this.bao = bao;
	}
	public List<Orders> getOrderlist() {
		return orderlist;
	}
	public void setOrderlist(List<Orders> orderlist) {
		this.orderlist = orderlist;
	}


	public String getStartTime() {
		return startTime;
	}


	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}


	public String getEndTime() {
		return endTime;
	}


	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}


	public int getPage() {
		return page;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public String getUname() {
		return uname;
	}


	public void setUname(String uname) {
		this.uname = uname;
	}


	public String getOrderstate() {
		return orderstate;
	}


	public void setOrderstate(String orderstate) {
		this.orderstate = orderstate;
	}


	public int getPagecount() {
		return pagecount;
	}


	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public List<Trade> getTrades() {
		return trades;
	}

	public void setTrades(List<Trade> trades) {
		this.trades = trades;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}


	
	   
}
